﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamSystem.Mdeol;
using ExamSystem.BLL;
namespace ExamSystem.ht
{
    public partial class TStudent_Add : System.Web.UI.Page
    {
        private TstudentBLL tb = new TstudentBLL();
        private TclassBLL tbl = new TclassBLL();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                List<Tclass> list = tbl.selecttclass();
                Tclass kt = new Tclass(0, "--请选择--");
                list.Insert(0, kt);
                this.tcId.DataSource = list;
                this.tcId.DataTextField = "tcno";
                this.tcId.DataValueField = "tcid";
                this.tcId.DataBind();
            }
        }

       
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.tcId.Value))
                {
                    this.lbMsg.InnerText = "请选择班级";
                    return;
                }
                int tcid = int.Parse(this.tcId.Value);//班级编号
                string tsname = this.tsName.Value;//学生名称
                string tsno = this.tsNo.Value;//学生学号
                string tspwd = this.tsPwd.Value;//学生密码
                string tssex = this.sex1.Value;//性别
                if(sex2.Checked)
                {
                    tssex = this.sex2.Value;
                }
                string tsphone = this.tsPhone.Value;//电话号码

                //验证
                if (string.IsNullOrEmpty(tsname))
                {
                    this.lbMsg.InnerText = "请输入学生名称";
                    return;
                }
                if (string.IsNullOrEmpty(tsno))
                {
                    this.lbMsg.InnerText = "请输入学生学号";
                    return;
                }
                else
                {
                    if(tb.selecttsno(tsno)>0)
                    {
                        this.lbMsg.InnerText = "学生学号重复";
                        return;
                    }
                }
                if (string.IsNullOrEmpty(tspwd))
                {
                    this.lbMsg.InnerText = "请输入学生密码";
                    return;
                }
                else
                {
                    if(tspwd.Length<6&&tspwd.Length>20)
                    {
                        this.lbMsg.InnerText = "密码6-20之间";
                        return;
                    }
                }
                if (string.IsNullOrEmpty(tsphone))
                {
                    this.lbMsg.InnerText = "请输入电话号码";
                    return;
                }
                else
                {
                    if(tsphone.Length!=11)
                    {
                        this.lbMsg.InnerText = "电话号码为11位";
                        return;
                    }
                }
                
                //入库

                Tclass tk = new Tclass(tcid,null);
                Tstudent tq = new Tstudent(0,tk,tsno,tsname,tspwd,tssex,tsphone);
                int b = tb.insert(tq);
                if (b > 0)
                {
                    util.FunHelper.LayerAlter(this, "添加成功", "TStudent_List.aspx");
                }
                else
                {
                    util.FunHelper.LayerAlter(this, "添加失败");
                }
            }
            catch (Exception ex)
            {
                util.FunHelper.LayerAlter(this, ex + "");
            }
        }
    }
}